package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.ArticleCommentCommentDao;
import com.jbp.exception.RRException;
import com.jbp.model.*;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.Date;
import java.util.Map;

/**
 * 评论的评论(ArticleCommentComment)表服务实现类
 * @author cc
 * @since 2020-08-25 14:37:34
 */
@Service
public class ArticleCommentCommentServiceImpl extends ServiceImpl<ArticleCommentCommentDao, ArticleCommentComment> implements ArticleCommentCommentService {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private KeywordService keywordService;
    @Autowired
    private UserLevelService userLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ArticleCommentComment> queryWrapper = new QueryWrapper<>();
        if (params.get("commentId") == null){
            throw new RRException("评论id不能为空");
        }
        queryWrapper.eq("comment_id",params.get("commentId"));
        queryWrapper.eq("status","normal");
        queryWrapper.orderByDesc("update_time");
        IPage<ArticleCommentComment> page = this.page(
                new Query<ArticleCommentComment>().getPage(params),
                queryWrapper
        );

        for (ArticleCommentComment articleCommentComment : page.getRecords()) {
            User reader = userService.getById(articleCommentComment.getReaderId());
            if (reader!=null) {
                articleCommentComment.setReaderName(reader.getName());
                articleCommentComment.setReaderImg(reader.getImg());
                articleCommentComment.setReaderSex(reader.getSex());
                articleCommentComment.setReaderLevel(reader.getLevel());
                articleCommentComment.setReaderLevelOn(userLevelService.levelOn(reader.getId()));
            }

            articleCommentComment.setContent(keywordService.replaceKeyWord(articleCommentComment.getContent()));

            if (articleCommentComment.getReplyUserId()!=null) {
                User reply = userService.getById(articleCommentComment.getReplyUserId());
                if (reply != null) {
                    articleCommentComment.setReplyUserName(reply.getName());
                }
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void remove(Integer articleCommentCommentId) {
        ArticleCommentComment articleCommentComment = getById(articleCommentCommentId);
        if (articleCommentComment != null)  {
            articleCommentComment.setStatus("del");
            updateById(articleCommentComment);
        }
    }

    @Override
    public void add(ArticleCommentComment articleCommentComment) {
        ArticleComment comment = articleCommentService.getById(articleCommentComment.getCommentId());
        if (comment==null) {
            throw new RRException("评论id不能为空");
        }
        save(articleCommentComment);

        comment.setCommentCount(comment.getCommentCount()+1);
        articleCommentService.updateById(comment);

        User reader = userService.getById(articleCommentComment.getReaderId());
        if (reader!=null) {
            articleCommentComment.setReaderName(reader.getName());
            articleCommentComment.setReaderImg(reader.getImg());
            articleCommentComment.setReaderSex(reader.getSex());
            articleCommentComment.setReaderLevel(reader.getLevel());
            articleCommentComment.setReaderLevelOn(userLevelService.levelOn(reader.getId()));
        }

        if (articleCommentComment.getReplyUserId()!=null) {
            User reply = userService.getById(articleCommentComment.getReplyUserId());
            articleCommentComment.setReplyUserName(reply.getName());
        }
        articleCommentComment.setLikeCount(0);
        articleCommentComment.setCommentCount(0);
        articleCommentComment.setCreateTime(new Date());

    }
}